package com.semidata.trp.report.utils;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.jdbc.SqlRunner;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import com.semidata.rtd.core.log.Logger;
import com.semidata.trp.report.report.ReportData;
import com.semidata.trp.report.report.TotalReportUnit;

public class SessionUtils {
    private static SqlSessionFactory sessionFactory = null;
	private static Integer[] mediaTypeList = { 0, 1, 2 };
    static {
        String resource = "conf/mybatis-report.xml";
        InputStream inputStream = null;
        try {
            inputStream = Resources.getResourceAsStream(resource);
        } catch (IOException e) {
            e.printStackTrace();
        }
        sessionFactory = new SqlSessionFactoryBuilder().build(new InputStreamReader(inputStream));
    }

    // 为了灵活获取session
    public static SqlSessionFactory getSessionFactory() {
        return sessionFactory;
    }

    public static SqlSession getSession() {
        return sessionFactory.openSession();
    }

    public static void close(SqlSession session) {
        
        if (session != null) {
            session.close();
        }
    }
    
    public static void executeSql(String... sqls) {
        SqlSession session = SessionUtils.getSession();
        SqlRunner runner = new SqlRunner(session.getConnection());
        try {
            runner.run("start transaction");
            for (String sql : sqls){
            	if(sql != "")
                    runner.run(sql);
            }
            runner.run("commit");
        } catch (SQLException e) {
            e.printStackTrace();
            try {
                runner.run("rollback");
            } catch (SQLException e1) {
                e1.printStackTrace();
            }
        } finally {
            session.close();
        }
    }

	public static void clearSql(String date, int dateType) {
		List<String> sqls = new ArrayList<String>();
		StringBuffer sql = new StringBuffer();
		String sqlHeader = "delete from ";
		String tableHeader = "";
		String dateTag = "";
		switch (dateType) {
		case 1:
			tableHeader = "trp_daily";
			dateTag = "date";
			break;
		case 2:
			tableHeader = "trp_weekly";
			dateTag = "end";
			break;
		case 3:
			tableHeader = "trp_monthly";
			dateTag = "month";
			break;
		}
		sqlHeader = sqlHeader.concat(tableHeader);
		sql.setLength(0);
		sql.append(sqlHeader);
		sql.append("report");
		sql.append(" where ");
		sql.append(dateTag);
		sql.append("=\"");
		sql.append(date);
		sql.append("\";");
		sqls.add(sql.toString());

		sql.setLength(0);
		sql.append(sqlHeader);
		sql.append("mediareport");
		sql.append(" where ");
		sql.append(dateTag);
		sql.append("=\"");
		sql.append(date);
		sql.append("\";");
		sqls.add(sql.toString());

		sql.setLength(0);
		sql.append(sqlHeader);
		sql.append("mediatypereport");
		sql.append(" where ");
		sql.append(dateTag);
		sql.append("=\"");
		sql.append(date);
		sql.append("\";");
		sqls.add(sql.toString());

		sql.setLength(0);
		sql.append(sqlHeader);
		sql.append("organizationmediareport");
		sql.append(" where ");
		sql.append(dateTag);
		sql.append("=\"");
		sql.append(date);
		sql.append("\";");
		sqls.add(sql.toString());

		sql.setLength(0);
		sql.append(sqlHeader);
		sql.append("organizationmediatypereport");
		sql.append(" where ");
		sql.append(dateTag);
		sql.append("=\"");
		sql.append(date);
		sql.append("\";");
		sqls.add(sql.toString());

		sql.setLength(0);
		sql.append(sqlHeader);
		sql.append("organizationpositionreport");
		sql.append(" where ");
		sql.append(dateTag);
		sql.append("=\"");
		sql.append(date);
		sql.append("\";");
		sqls.add(sql.toString());

		sql.setLength(0);
		sql.append(sqlHeader);
		sql.append("organizationreport");
		sql.append(" where ");
		sql.append(dateTag);
		sql.append("=\"");
		sql.append(date);
		sql.append("\";");
		sqls.add(sql.toString());

		sql.setLength(0);
		sql.append(sqlHeader);
		sql.append("positionreport");
		sql.append(" where ");
		sql.append(dateTag);
		sql.append("=\"");
		sql.append(date);
		sql.append("\";");
		sqls.add(sql.toString());

		sql.setLength(0);
		sql.append(sqlHeader);
		sql.append("strategypositionreport");
		sql.append(" where ");
		sql.append(dateTag);
		sql.append("=\"");
		sql.append(date);
		sql.append("\";");
		sqls.add(sql.toString());

		sql.setLength(0);
		sql.append(sqlHeader);
		sql.append("strategymediatypereport");
		sql.append(" where ");
		sql.append(dateTag);
		sql.append("=\"");
		sql.append(date);
		sql.append("\";");
		sqls.add(sql.toString());

		sql.setLength(0);
		sql.append(sqlHeader);
		sql.append("strategyreport");
		sql.append(" where ");
		sql.append(dateTag);
		sql.append("=\"");
		sql.append(date);
		sql.append("\";");
		sqls.add(sql.toString());

		sql.setLength(0);
		sql.append(sqlHeader);
		sql.append("resourcereport");
		sql.append(" where ");
		sql.append(dateTag);
		sql.append("=\"");
		sql.append(date);
		sql.append("\";");
		sqls.add(sql.toString());

		sql.setLength(0);
		sql.append(sqlHeader);
		sql.append("strategyresourcereport");
		sql.append(" where ");
		sql.append(dateTag);
		sql.append("=\"");
		sql.append(date);
		sql.append("\";");
		sqls.add(sql.toString());
		/*
		 * sql.setLength(0); sql.append(sqlHeader);
		 * sql.append("organizationresourcereport"); sql.append(" where ");
		 * sql.append(dateTag); sql.append("=\""); sql.append(date);
		 * sql.append("\";"); sqls[13] = sql.toString();
		 */
		for (int i = 0; i < sqls.size(); i++) {
			Logger.log("DEBUG", sqls.get(i));
		}

		// SessionUtils.executeSql(sqls);
		SessionUtils
				.executeSql((String[]) sqls.toArray(new String[sqls.size()]));
	}

	public static String getFirstDayOf7Days(String date) {
		SimpleDateFormat dateformat = new SimpleDateFormat("yyyyMMdd");
		Calendar c = new GregorianCalendar();
		c.set(Integer.valueOf(date.substring(0, 4)),
				Integer.valueOf(date.substring(4, 6)) - 1,
				Integer.valueOf(date.substring(6)) - 6);
		String firstDayOf7Days = dateformat.format(c.getTime());
		dateformat = null;
		c = null;
		return firstDayOf7Days;
	}

	public static void update(String date, int dateType,ReportData reportData) {
		// Logger.log("DEBUG", String.valueOf(dateType));
		int valuesCount = 0;
		List<String> sqls = new ArrayList<String>();
		String sqlHeader = "insert into ";
		String tableHeader = "";
		StringBuffer sql = new StringBuffer();
		String dateTag = "";
		String firstDay = "";
		switch (dateType) {
		case 1:
			tableHeader = "trp_daily";
			dateTag = "date";
			break;
		case 2:
			tableHeader = "trp_weekly";
			dateTag = "start,end";
			firstDay = getFirstDayOf7Days(date);
			break;
		case 3:
			tableHeader = "trp_monthly";
			dateTag = "month";
			break;
		}
		sqlHeader = sqlHeader.concat(tableHeader);
		TotalReportUnit totalReportUnit = reportData.getTotalReport();

		if (totalReportUnit.getPv() > 0 || totalReportUnit.getClick() > 0) {
			sql.setLength(0);
			sql.append(sqlHeader);
			sql.append("report");
			sql.append("(pv,uv,click,cost,");
			sql.append(dateTag);
			sql.append(") values (");
			sql.append(totalReportUnit.getPv());
			sql.append(",");
			sql.append(totalReportUnit.getUv());
			sql.append(",");
			sql.append(totalReportUnit.getClick());
			sql.append(",");
			sql.append(totalReportUnit.getCost());
			sql.append(",\"");
			if (dateType == 2) {
				sql.append(firstDay);
				sql.append("\",\"");
			}
			sql.append(date);
			sql.append("\");");
			sqls.add(sql.toString());

			if (reportData.getMediaReport() == null
					|| reportData.getMediaReport().size() == 0) {
				;
			} else {
				valuesCount = 0;
				sql.setLength(0);
				for (Map.Entry<Integer, TotalReportUnit> entryReportData : reportData
						.getMediaReport().entrySet()) {
					if (valuesCount % 1000 == 0) {
						if (sql.length() > 0) {
							sql.delete(sql.length() - 1, sql.length());
							sqls.add(sql.toString());
						}
						sql.setLength(0);
						sql.append(sqlHeader);
						sql.append("mediareport");
						sql.append("(media_id,pv,uv,click,cost,");
						sql.append(dateTag);
						sql.append(") values ");
					}
					sql.append("(");
					sql.append(entryReportData.getKey());
					sql.append(",");
					totalReportUnit = entryReportData.getValue();
					sql.append(totalReportUnit.getPv());
					sql.append(",");
					sql.append(totalReportUnit.getUv());
					sql.append(",");
					sql.append(totalReportUnit.getClick());
					sql.append(",");
					sql.append(totalReportUnit.getCost());
					sql.append(",\"");
					if (dateType == 2) {
						sql.append(firstDay);
						sql.append("\",\"");
					}
					sql.append(date);
					sql.append("\"),");
					valuesCount = valuesCount + 1;
				}
				sql.delete(sql.length() - 1, sql.length());
				sqls.add(sql.toString());
			}

			if (reportData.getMediaTypeReport() == null
					|| reportData.getMediaTypeReport().size() == 0) {
				;
			} else {
				valuesCount = 0;
				sql.setLength(0);
				sql.append(sqlHeader);
				sql.append("mediatypereport");
				sql.append("(type,pv,uv,click,cost,");
				sql.append(dateTag);
				sql.append(") values ");
				for (int t = 0; t < mediaTypeList.length; t++) {
					sql.append("(");
					sql.append(mediaTypeList[t]);
					sql.append(",");
					if (reportData.getMediaTypeReport().get(mediaTypeList[t]) != null) {
						totalReportUnit = reportData.getMediaTypeReport().get(
								mediaTypeList[t]);
						sql.append(totalReportUnit.getPv());
						sql.append(",");
						sql.append(totalReportUnit.getUv());
						sql.append(",");
						sql.append(totalReportUnit.getClick());
						sql.append(",");
						sql.append(totalReportUnit.getCost());
					} else {
						sql.append("0,0,0,0");
					}
					sql.append(",\"");
					if (dateType == 2) {
						sql.append(firstDay);
						sql.append("\",\"");
					}
					sql.append(date);
					sql.append("\"),");
				}
				sql.delete(sql.length() - 1, sql.length());
				sqls.add(sql.toString());
			}

			if (reportData.getPosReport() == null
					|| reportData.getPosReport().size() == 0) {
				;
			} else {
				valuesCount = 0;
				sql.setLength(0);
				for (Map.Entry<Integer, TotalReportUnit> entryReportData : reportData
						.getPosReport().entrySet()) {
					if (valuesCount % 1000 == 0) {
						if (sql.length() > 0) {
							sql.delete(sql.length() - 1, sql.length());
							sqls.add(sql.toString());
						}
						sql.setLength(0);
						sql.append(sqlHeader);
						sql.append("positionreport");
						sql.append("(position_id,pv,uv,click,cost,");
						sql.append(dateTag);
						sql.append(") values ");
					}
					sql.append("(");
					sql.append(entryReportData.getKey());
					sql.append(",");
					totalReportUnit = entryReportData.getValue();
					sql.append(totalReportUnit.getPv());
					sql.append(",");
					sql.append(totalReportUnit.getUv());
					sql.append(",");
					sql.append(totalReportUnit.getClick());
					sql.append(",");
					sql.append(totalReportUnit.getCost());
					sql.append(",\"");
					if (dateType == 2) {
						sql.append(firstDay);
						sql.append("\",\"");
					}
					sql.append(date);
					sql.append("\"),");
					valuesCount = valuesCount + 1;
				}
				sql.delete(sql.length() - 1, sql.length());
				sqls.add(sql.toString());
			}

			if (reportData.getOrgReport() == null
					|| reportData.getOrgReport().size() == 0) {
				;
			} else {
				valuesCount = 0;
				sql.setLength(0);
				for (Map.Entry<Integer, TotalReportUnit> entryReportData : reportData
						.getOrgReport().entrySet()) {
					if (valuesCount % 1000 == 0) {
						if (sql.length() > 0) {
							sql.delete(sql.length() - 1, sql.length());
							sqls.add(sql.toString());
						}
						sql.setLength(0);
						sql.append(sqlHeader);
						sql.append("organizationreport");
						sql.append("(organization_id,pv,uv,click,cost,");
						sql.append(dateTag);
						sql.append(") values ");
					}
					sql.append("(");
					sql.append(entryReportData.getKey());
					sql.append(",");
					totalReportUnit = entryReportData.getValue();
					sql.append(totalReportUnit.getPv());
					sql.append(",");
					sql.append(totalReportUnit.getUv());
					sql.append(",");
					sql.append(totalReportUnit.getClick());
					sql.append(",");
					sql.append(totalReportUnit.getCost());
					sql.append(",\"");
					if (dateType == 2) {
						sql.append(firstDay);
						sql.append("\",\"");
					}
					sql.append(date);
					sql.append("\"),");
					valuesCount = valuesCount + 1;
				}
				sql.delete(sql.length() - 1, sql.length());
				sqls.add(sql.toString());
			}

			if (reportData.getOrgMediaReport() == null
					|| reportData.getOrgMediaReport().size() == 0) {
				;
			} else {
				valuesCount = 0;
				sql.setLength(0);
				for (Map.Entry<Integer, Map<Integer, TotalReportUnit>> entryOrgMediaReport : reportData
						.getOrgMediaReport().entrySet()) {
					int fKey = entryOrgMediaReport.getKey();
					for (Map.Entry<Integer, TotalReportUnit> entryMediaReport : reportData
							.getOrgMediaReport().get(fKey).entrySet()) {
						if (valuesCount % 1000 == 0) {
							if (sql.length() > 0) {
								sql.delete(sql.length() - 1, sql.length());
								sqls.add(sql.toString());
							}
							sql.setLength(0);
							sql.append(sqlHeader);
							sql.append("organizationmediareport");
							sql.append("(organization_id,media_id,pv,uv,click,cost,");
							sql.append(dateTag);
							sql.append(") values ");
						}
						sql.append("(");
						sql.append(fKey);
						sql.append(",");
						sql.append(entryMediaReport.getKey());
						sql.append(",");
						totalReportUnit = entryMediaReport.getValue();
						sql.append(totalReportUnit.getPv());
						sql.append(",");
						sql.append(totalReportUnit.getUv());
						sql.append(",");
						sql.append(totalReportUnit.getClick());
						sql.append(",");
						sql.append(totalReportUnit.getCost());
						sql.append(",\"");
						if (dateType == 2) {
							sql.append(firstDay);
							sql.append("\",\"");
						}
						sql.append(date);
						sql.append("\"),");
						valuesCount = valuesCount + 1;
					}
				}
				sql.delete(sql.length() - 1, sql.length());
				sqls.add(sql.toString());
			}

			if (reportData.getOrgMediaTypeReport() == null
					|| reportData.getOrgMediaTypeReport().size() == 0) {
				;
			} else {
				valuesCount = 0;
				sql.setLength(0);
				for (Map.Entry<Integer, Map<Integer, TotalReportUnit>> entryOrgMediaReport : reportData
						.getOrgMediaTypeReport().entrySet()) {
					int fKey = entryOrgMediaReport.getKey();
					for (int t = 0; t < mediaTypeList.length; t++) {
						if (valuesCount % 1000 == 0) {
							if (sql.length() > 0) {
								sql.delete(sql.length() - 1, sql.length());
								sqls.add(sql.toString());
							}
							sql.setLength(0);
							sql.append(sqlHeader);
							sql.append("organizationmediatypereport");
							sql.append("(organization_id,type,pv,uv,click,cost,");
							sql.append(dateTag);
							sql.append(") values ");
						}
						sql.append("(");
						sql.append(fKey);
						sql.append(",");
						sql.append(mediaTypeList[t]);
						sql.append(",");
						if (entryOrgMediaReport.getValue()
								.get(mediaTypeList[t]) != null) {
							totalReportUnit = entryOrgMediaReport.getValue()
									.get(mediaTypeList[t]);
							sql.append(totalReportUnit.getPv());
							sql.append(",");
							sql.append(totalReportUnit.getUv());
							sql.append(",");
							sql.append(totalReportUnit.getClick());
							sql.append(",");
							sql.append(totalReportUnit.getCost());
						} else {
							sql.append("0,0,0,0");
						}
						sql.append(",\"");
						if (dateType == 2) {
							sql.append(firstDay);
							sql.append("\",\"");
						}
						sql.append(date);
						sql.append("\"),");
						valuesCount = valuesCount + 1;
					}
				}
				sql.delete(sql.length() - 1, sql.length());
				sqls.add(sql.toString());
			}

			if (reportData.getOrgPosReport() == null
					|| reportData.getOrgPosReport().size() == 0) {
				;
			} else {
				valuesCount = 0;
				sql.setLength(0);
				for (Map.Entry<Integer, Map<Integer, TotalReportUnit>> entryOrgPosReport : reportData
						.getOrgPosReport().entrySet()) {
					int fKey = entryOrgPosReport.getKey();
					for (Map.Entry<Integer, TotalReportUnit> entryPosReport : reportData
							.getOrgPosReport().get(fKey).entrySet()) {
						if (valuesCount % 1000 == 0) {
							if (sql.length() > 0) {
								sql.delete(sql.length() - 1, sql.length());
								sqls.add(sql.toString());
							}
							sql.setLength(0);
							sql.append(sqlHeader);
							sql.append("organizationpositionreport");
							sql.append("(organization_id,position_id,pv,uv,click,cost,");
							sql.append(dateTag);
							sql.append(") values ");
						}
						sql.append("(");
						sql.append(fKey);
						sql.append(",");
						sql.append(entryPosReport.getKey());
						sql.append(",");
						totalReportUnit = entryPosReport.getValue();
						sql.append(totalReportUnit.getPv());
						sql.append(",");
						sql.append(totalReportUnit.getUv());
						sql.append(",");
						sql.append(totalReportUnit.getClick());
						sql.append(",");
						sql.append(totalReportUnit.getCost());
						sql.append(",\"");
						if (dateType == 2) {
							sql.append(firstDay);
							sql.append("\",\"");
						}
						sql.append(date);
						sql.append("\"),");
						valuesCount = valuesCount + 1;
					}
				}
				sql.delete(sql.length() - 1, sql.length());
				sqls.add(sql.toString());
			}

			if (reportData.getOrgStraReport() == null
					|| reportData.getOrgStraReport().size() == 0) {
				;
			} else {
				valuesCount = 0;
				sql.setLength(0);
				for (Map.Entry<Integer, Map<Integer, TotalReportUnit>> entryOrgStraReport : reportData
						.getOrgStraReport().entrySet()) {
					int fKey = entryOrgStraReport.getKey();
					for (Map.Entry<Integer, TotalReportUnit> entryStraReport : reportData
							.getOrgStraReport().get(fKey).entrySet()) {
						if (valuesCount % 1000 == 0) {
							if (sql.length() > 0) {
								sql.delete(sql.length() - 1, sql.length());
								sqls.add(sql.toString());
							}
							sql.setLength(0);
							sql.append(sqlHeader);
							sql.append("strategyreport");
							sql.append("(strategy_id,pv,uv,click,cost,");
							sql.append(dateTag);
							sql.append(") values ");
						}
						sql.append("(");
						sql.append(entryStraReport.getKey());
						sql.append(",");
						totalReportUnit = entryStraReport.getValue();
						sql.append(totalReportUnit.getPv());
						sql.append(",");
						sql.append(totalReportUnit.getUv());
						sql.append(",");
						sql.append(totalReportUnit.getClick());
						sql.append(",");
						sql.append(totalReportUnit.getCost());
						sql.append(",\"");
						if (dateType == 2) {
							sql.append(firstDay);
							sql.append("\",\"");
						}
						sql.append(date);
						sql.append("\"),");
						valuesCount = valuesCount + 1;
					}
				}
				sql.delete(sql.length() - 1, sql.length());
				sqls.add(sql.toString());
			}

			if (reportData.getStraPosReport() == null
					|| reportData.getStraPosReport().size() == 0) {
				;
			} else {
				valuesCount = 0;
				sql.setLength(0);
				for (Map.Entry<Integer, Map<Integer, TotalReportUnit>> entryStraPosReport : reportData
						.getStraPosReport().entrySet()) {
					int fKey = entryStraPosReport.getKey();
					for (Map.Entry<Integer, TotalReportUnit> entryPosReport : reportData
							.getStraPosReport().get(fKey).entrySet()) {
						if (valuesCount % 1000 == 0) {
							if (sql.length() > 0) {
								sql.delete(sql.length() - 1, sql.length());
								sqls.add(sql.toString());
							}
							sql.setLength(0);
							sql.append(sqlHeader);
							sql.append("strategypositionreport");
							sql.append("(strategy_id,position_id,pv,uv,click,cost,");
							sql.append(dateTag);
							sql.append(") values ");
						}
						sql.append("(");
						sql.append(fKey);
						sql.append(",");
						sql.append(entryPosReport.getKey());
						sql.append(",");
						totalReportUnit = entryPosReport.getValue();
						sql.append(totalReportUnit.getPv());
						sql.append(",");
						sql.append(totalReportUnit.getUv());
						sql.append(",");
						sql.append(totalReportUnit.getClick());
						sql.append(",");
						sql.append(totalReportUnit.getCost());
						sql.append(",\"");
						if (dateType == 2) {
							sql.append(firstDay);
							sql.append("\",\"");
						}
						sql.append(date);
						sql.append("\"),");
						valuesCount = valuesCount + 1;
					}
				}
				sql.delete(sql.length() - 1, sql.length());
				sqls.add(sql.toString());
			}

			if (reportData.getStraMediaTypeReport() == null
					|| reportData.getStraMediaTypeReport().size() == 0) {
				;
			} else {
				valuesCount = 0;
				sql.setLength(0);
				for (Map.Entry<Integer, Map<Integer, TotalReportUnit>> entryStraMediaTypeReport : reportData
						.getStraMediaTypeReport().entrySet()) {
					int fKey = entryStraMediaTypeReport.getKey();
					for (int t = 0; t < mediaTypeList.length; t++) {
						if (valuesCount % 1000 == 0) {
							if (sql.length() > 0) {
								sql.delete(sql.length() - 1, sql.length());
								sqls.add(sql.toString());
							}
							sql.setLength(0);
							sql.append(sqlHeader);
							sql.append("strategymediatypereport");
							sql.append("(strategy_id,type,pv,uv,click,cost,");
							sql.append(dateTag);
							sql.append(") values ");
						}
						sql.append("(");
						sql.append(fKey);
						sql.append(",");
						sql.append(mediaTypeList[t]);
						sql.append(",");
						if (entryStraMediaTypeReport.getValue().get(
								mediaTypeList[t]) != null) {
							totalReportUnit = entryStraMediaTypeReport
									.getValue().get(mediaTypeList[t]);
							sql.append(totalReportUnit.getPv());
							sql.append(",");
							sql.append(totalReportUnit.getUv());
							sql.append(",");
							sql.append(totalReportUnit.getClick());
							sql.append(",");
							sql.append(totalReportUnit.getCost());
						} else {
							sql.append("0,0,0,0");
						}
						sql.append(",\"");
						if (dateType == 2) {
							sql.append(firstDay);
							sql.append("\",\"");
						}
						sql.append(date);
						sql.append("\"),");
						valuesCount = valuesCount + 1;
					}
				}
				sql.delete(sql.length() - 1, sql.length());
				sqls.add(sql.toString());
			}

			if (reportData.getResReport() == null
					|| reportData.getResReport().size() == 0) {
				;
			} else {
				valuesCount = 0;
				sql.setLength(0);
				for (Map.Entry<Integer, TotalReportUnit> entryReportData : reportData
						.getResReport().entrySet()) {
					if (valuesCount % 1000 == 0) {
						if (sql.length() > 0) {
							sql.delete(sql.length() - 1, sql.length());
							sqls.add(sql.toString());
						}
						sql.setLength(0);
						sql.append(sqlHeader);
						sql.append("resourcereport");
						sql.append("(resource_id,pv,uv,click,cost,");
						sql.append(dateTag);
						sql.append(") values ");
					}
					sql.append("(");
					sql.append(entryReportData.getKey());
					sql.append(",");
					totalReportUnit = entryReportData.getValue();
					sql.append(totalReportUnit.getPv());
					sql.append(",");
					sql.append(totalReportUnit.getUv());
					sql.append(",");
					sql.append(totalReportUnit.getClick());
					sql.append(",");
					sql.append(totalReportUnit.getCost());
					sql.append(",\"");
					if (dateType == 2) {
						sql.append(firstDay);
						sql.append("\",\"");
					}
					sql.append(date);
					sql.append("\"),");
					valuesCount = valuesCount + 1;
				}
				sql.delete(sql.length() - 1, sql.length());
				sqls.add(sql.toString());
			}

			if (reportData.getStraResReport() == null
					|| reportData.getStraResReport().size() == 0) {
				;
			} else {
				valuesCount = 0;
				sql.setLength(0);
				for (Map.Entry<Integer, Map<Integer, TotalReportUnit>> entryOrgMediaReport : reportData
						.getStraResReport().entrySet()) {
					int fKey = entryOrgMediaReport.getKey();
					for (Map.Entry<Integer, TotalReportUnit> entryResReport : reportData
							.getStraResReport().get(fKey).entrySet()) {
						if (valuesCount % 1000 == 0) {
							if (sql.length() > 0) {
								sql.delete(sql.length() - 1, sql.length());
								sqls.add(sql.toString());
							}
							sql.setLength(0);
							sql.append(sqlHeader);
							sql.append("strategyresourcereport");
							sql.append("(strategy_id,resource_id,pv,uv,click,cost,");
							sql.append(dateTag);
							sql.append(") values ");
						}
						sql.append("(");
						sql.append(fKey);
						sql.append(",");
						sql.append(entryResReport.getKey());
						sql.append(",");
						totalReportUnit = entryResReport.getValue();
						sql.append(totalReportUnit.getPv());
						sql.append(",");
						sql.append(totalReportUnit.getUv());
						sql.append(",");
						sql.append(totalReportUnit.getClick());
						sql.append(",");
						sql.append(totalReportUnit.getCost());
						sql.append(",\"");
						if (dateType == 2) {
							sql.append(firstDay);
							sql.append("\",\"");
						}
						sql.append(date);
						sql.append("\"),");
						valuesCount = valuesCount + 1;
					}
				}
				sql.delete(sql.length() - 1, sql.length());
				sqls.add(sql.toString());
			}

			for (int i = 0; i < sqls.size(); i++) {
				Logger.log("DEBUG", sqls.get(i));
			}
			
			clearSql(date, dateType);

			SessionUtils.executeSql((String[]) sqls.toArray(new String[sqls
					.size()]));
		}
	}
    
    public static void main(String[] args) {
        executeSql("insert into trp_area (name, code) values('test3', 'test3')", "insert into trp_area (name, code) values('test4', 'test4')");        
    }
}
